import http from '@/utils/http'

/** 任务类型 */
export interface JobResp {
  jobId: number
  jobGroup: number
  jobName: string
  description?: string
  triggerType: number
  triggerInterval: string | number
  executorType: number
  taskType: number
  executorInfo: string
  argsStr?: string
  argsType?: string
  routeKey: number
  blockStrategy: number
  executorTimeout: number
  maxRetryTimes: number
  retryInterval: number
  parallelNum: number
  status: number
  nextTriggerAt?: Date
  createDt?: Date
  updateDt?: Date
}
export interface JobQuery {
  jobName?: string
  jobGroup: ''
  status?: number
}
export interface JobPageQuery extends JobQuery, PageQuery {}

/** 任务日志类型 */
export interface JobLogResp {
  id: number
  jobID?: number
  jobName: string
  nodeID?: string
  jobGroup?: string
  args?: string
  status?: number
  errorMsg?: string
  startTime?: string
  duration?: number
  endTime?: string
}
export interface JobLogQuery {
  jobId?: number
  jobGroup?: number
  jobName?: string
  status?: number
  datetimeRange?: Array<string>
}
export interface JobLogPageQuery extends JobLogQuery, PageQuery {}

/** 任务实例类型 */
export interface JobInstanceResp {
  id: number
  groupName: string
  jobId: number
  taskBatchId: number
  taskStatus: number
  retryCount: number
  resultMessage: string
  clientInfo: string
}
export interface JobInstanceQuery {
  jobId?: string | number
  taskBatchId?: number | string
}

/** 任务实例日志类型 */
export interface JobInstanceLogResp {
  id: number
  message: any[]
  finished: number
  fromIndex: number
  nextStartId: number
}
export interface JobInstanceLogQuery {
  taskBatchId: number
  jobId: number
  taskId: number
  startId: number
  fromIndex: number
  size: number
}

const BASE_URL = '/system/sysjob'

/** @desc 查询任务列表 */
export function listJob(query: JobPageQuery) {
  return http.get<PageRes<JobResp[]>>(`${BASE_URL}`, query)
}

/** @desc 新增任务 */
export function addJob(data: any) {
  return http.post(`${BASE_URL}`, data)
}

/** @desc 修改任务 */
export function updateJob(data: any, id: number) {
  return http.put(`${BASE_URL}/${id}`, data)
}

/** @desc 修改任务状态 */
export function updateJobStatus(data: any, id: number) {
  return http.patch(`${BASE_URL}/${id}/status`, data)
}

/** @desc 删除任务 */
export function deleteJob(id: number) {
  return http.del(`${BASE_URL}/${id}`)
}

/** @desc 执行任务 */
export function triggerJob(id: number) {
  return http.post(`${BASE_URL}/trigger/${id}`)
}
export function listJobLog(query: JobLogPageQuery) {
  return http.get<PageRes<JobLogResp[]>>(`${BASE_URL}/log`, query)
}
// 重试任务
export function retryJob(id: number) {
  console.log(`功能未实现:${id}`)
}
// 停止任务
export function stopJob(id: number) {
  console.log(`功能未实现:${id}`)
}
